<?php
/**
 * 备货系统推送消息到其他系统
 * @author sundongdong
 * @date 2016-07-25
 */
class Interface_IfsController extends Controller_Interface {
	// 请求的接口
	private $pms = '';
	private $db  = '';
	private $wdb = '';
	private $page_size	  = 300;
	
	public function init(){
		parent::init();
		$this->pms = Helper_Config::get('interface', 'pms', 'url');
		$this->db = Model_Db::getInstance();
		$this->wdb = Model_Db::getInstance('write_db');
	}
	/**
	 * 推送样衣需求到采购系统
	 */
	public function sendSampleClothToPmsAction() {
		$sync_url = $this->pms . "interface/requiredsample/syncpurchase";
		// 待同步总数
		$total_sql = "select count(1) from sample_cloth where sync_status = 0 and provide_status != 40";
		$total = $this->db->fetchOne($total_sql);
		if (!$total) 
			exit("没有未同步的样衣需求");
		// 开始同步
		$page = ceil($total/$this->page_size);
		for ($i = 1; $i<=$page; $i++) {
			$limit = " limit " . $this->page_size;
			$sql = "select id,datediff(now(),add_time) as diffday,sku,num as requirement_quantity,purchaser,add_time as create_date,purchase_type,warehouse_id,
				provide_qty, provide_status,add_user from sample_cloth where sync_status=0 and provide_status != 40";
			$sql .= $limit;
			$data = $this->db->fetchAll($sql);
			
			$skus = array(); //当前发送数据的sku
			if (count($data) < 1) {
				exit("样衣需求已经同步完成");
			}
			$purchase_goods = array();
			foreach ($data as $key => $val) {
				$goods_info = $this->db->fetchRow("select product_name,thumb_url,reference_price,product_status,recommend_level,catalog_id,available_stock,goods_resource_status,supplier_sn,sp_name as provider_name from p_product as p1,p_product_purchase as p2,p_product_supplier as p3 where p1.goods_sn = :sku and p2.goods_sn = :sku and p2.supplier_sn=p3.sp_code", array("sku"=>$val['sku']));
				if (!empty($goods_info)) {
					$skus[] = $val['sku'];
					
					$purchase_goods[$key] = $val;
					// 产品名称
					$purchase_goods[$key]['sku_name'] = $goods_info['product_name'];
					// 产品图片
					$purchase_goods[$key]['sku_img_thumb'] = $goods_info['thumb_url'];
					// 参考价格
					$purchase_goods[$key]['reference_price'] = $goods_info['reference_price'];
					// 历史价格
					$purchase_goods[$key]['last_lowest_price'] = '0.00';
					// 产品状态
					$purchase_goods[$key]['sku_status'] = $goods_info['product_status'];
					// 是否网采
					$purchase_goods[$key]['is_net_purchase'] = '0';
					// 分类ID
					$purchase_goods[$key]['cat_id'] = $goods_info['catalog_id'];
					// 供应商编码
					$purchase_goods[$key]['provider_sn'] = $goods_info['supplier_sn'];
					// 供应商名称
					$purchase_goods[$key]['provider_name'] = $goods_info['provider_name'];
					// 仓库ID
					$purchase_goods[$key]['stock_id'] = $val['warehouse_id'];
					// 货源状态
					$purchase_goods[$key]['sku_source_status'] = $goods_info['goods_resource_status'];
					//推荐级别
					$purchase_goods[$key]['recmd_level'] = $goods_info['recommend_level'];
					//可用库存
					$purchase_goods[$key]['available_stock'] = "";
					// 备货数量
					$purchase_goods[$key]['stock_num'] = 0;
					
					p($purchase_goods);die;
				}
			}
		}
	}
}